package com.zf.zfed.audit.tools.extension

import android.content.Context
import android.view.View
import android.view.ViewGroup

fun View.applyVisible() {
    visibility = View.VISIBLE
}

fun View.applyInvisible() {
    visibility = View.INVISIBLE
}

fun View.applyGone() {
    visibility = View.GONE
}

fun View.applyDisable() {
    isEnabled = false
}

fun View.applyEnable() {
    isEnabled = true
}

fun View.applySize(width: Int? = null, height: Int? = null) {
    if (null == width && null == height) return
    val lp = this.layoutParams ?: ViewGroup.LayoutParams(width ?: 0, height ?: 0)
    if (null != width) lp.width = width
    if (null != height) lp.height = height
    this.layoutParams = lp
}

fun View.appleHeight(height: Int) {
    val lp = this.layoutParams
    lp.height = height
    this.layoutParams = lp
}

inline fun View.singleClick(crossinline onClick: (v: View) -> Unit, delayMills: Long = 300) {
    this.setOnClickListener {
        this.isClickable = false
        onClick(this)
        this.postDelayed({
            this.isClickable = true
        }, delayMills)
    }
}

inline fun View.singleClick(delayMills: Long = 300, crossinline onClick: (v: View) -> Unit) {
    this.setOnClickListener {
        this.isClickable = false
        onClick(this)
        this.postDelayed({
            this.isClickable = true
        }, delayMills)
    }
}

fun Context.spFromDimen(id: Int): Float {
    return resources.getDimension(id) / resources.displayMetrics.scaledDensity
}

fun Context.dpFromDimen(id: Int): Int {
    return resources.getDimensionPixelSize(id)
}